package com.ibiaoke.goods.dao;

import com.google.common.collect.Lists;
import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.exception.ServiceException;
import com.ibiaoke.goods.consts.GoodsConsts;
import com.ibiaoke.goods.entity.*;
import com.ibiaoke.goods.entity.vo.XcxGoodsVO;
import com.ibiaoke.goods.mapper.XcxGoodsMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Repository
public class XcxGoodsDao extends BaseDao<XcxGoods, XcxGoodsExample, Integer, XcxGoodsMapper> {

    @Override
    public XcxGoodsMapper getMapper() {
        return xcxGoodsMapper;
    }

    @Autowired
    private XcxGoodsMapper xcxGoodsMapper;

    @Autowired
    private XcxShopPriceDao xcxShopPriceDao;


    public List<XcxGoods> xcxListGoodsByGoodsClass(Integer cateId, Integer shopId, Page page) {
        XcxGoodsExample example = new XcxGoodsExample();
        XcxGoodsExample.Criteria criteria = example.createCriteria().andDelStatusEqualTo(CommonConsts.NO)
                .andStatusEqualTo(GoodsConsts.STATUS_ON_SHELF);
        if (cateId != null) {
            criteria.andCateIdEqualTo(cateId);
        }
        page.setTotalCount(xcxGoodsMapper.countByExample(example));
        example.setLimitStart(page.limitStart());
        example.setLimitEnd(page.limitEnd());
        example.setOrderByClause("update_time desc");
        return filterShopPrice(xcxGoodsMapper.selectByExample(example), shopId);
    }

    /**
     * 商品店铺价格和库存设置
     *
     * @param xcxGoodsList 商品列表
     * @param shopId    店铺id
     */
    public List<XcxGoods> filterShopPrice(List<XcxGoods> xcxGoodsList, Integer shopId) {
        if (CollectionUtils.isEmpty(xcxGoodsList) || shopId == null) {
            return xcxGoodsList;
        }
        //获取所有商品id
        List<Integer> goodsIds = xcxGoodsList.stream().map(XcxGoods::getGoodsId).collect(Collectors.toList());
        //获取商品店铺价格
        List<XcxShopPrice> shopPrices = xcxShopPriceDao.listShopPrice(goodsIds, shopId);
        if (shopPrices == null || CollectionUtils.isEmpty(shopPrices)) {
            for (XcxGoods goods : xcxGoodsList) {
                goods.setStockNum(0);
                goods.setShopId(shopId);
            }
            return xcxGoodsList;
        }
        //为每个商品设置店铺价格和库存
        xcxGoodsList.forEach(xcxGood -> {
            shopPrices.forEach(shopPrice -> {
                //设置店铺价格和库存
                if (Objects.equals(xcxGood.getGoodsId(), shopPrice.getGoodsId())) {
                    xcxGood.setPrice(shopPrice.getPrice());
                    xcxGood.setPtPrice(shopPrice.getPtPrice() == null ? new BigDecimal("0") : shopPrice.getPtPrice());
                    xcxGood.setStockNum(Integer.parseInt(new java.text.DecimalFormat("0").format(shopPrice.getStockNum())));
                    xcxGood.setPtType(shopPrice.getPtType() == null ? 0 : Integer.valueOf(shopPrice.getPtType()));
                    xcxGood.setDeliverytype(shopPrice.getDeliveryType() == null ? 0 : Integer.valueOf(shopPrice.getDeliveryType()));
                    xcxGood.setDeliveryPrice(shopPrice.getDeliveryPrice() == null ? new BigDecimal(0) : new BigDecimal(shopPrice.getDeliveryPrice()));
                }
            });
            xcxGood.setShopId(shopId);
        });
        return xcxGoodsList;
    }

    public XcxGoods getXcxGoodsByGoodsId(Integer goodsId, Integer shopId){
        XcxGoods xcxGoods = selectByPrimaryKey(goodsId);
        return filterOneShopPrice(xcxGoods, shopId);
    }


    public XcxGoods filterOneShopPrice(XcxGoods goods, Integer shopId) {
        ArrayList<XcxGoods> goodsList = Lists.newArrayList(goods);
        return filterShopPrice(goodsList, shopId).get(0);
    }

    public int  updateXcxGoodSaleNum(Integer id){
        XcxGoods xcxGoods = selectByPrimaryKey(id);
        xcxGoods.setSaleNum(xcxGoods.getSaleNum()+1);
        return updateByPrimaryKeySelective(xcxGoods);
    }

    public int  updateXcxGoodSaleNumByNum(Integer id,Integer saleNum){
        XcxGoods xcxGoods = selectByPrimaryKey(id);
        xcxGoods.setSaleNum(xcxGoods.getSaleNum()+saleNum);
        return updateByPrimaryKeySelective(xcxGoods);
    }

    public int updatePtXcxGoodSaleNum(XcxGoods xcxGood){
        xcxGood.setSaleNum(xcxGood.getSaleNum()+xcxGood.getPtType());
        return updateByPrimaryKeySelective(xcxGood);
    }

    public List<XcxGoods> listXcxGoods(XcxGoodsVO xcxGoodsVO, Page page) {
        XcxGoodsExample example = new XcxGoodsExample();
        XcxGoodsExample.Criteria criteria = example.createCriteria();
        //商品ID
        if (xcxGoodsVO.getGoodsId() != null) {
            criteria.andGoodsIdEqualTo(xcxGoodsVO.getGoodsId());
        }
        //商品名称
        if (xcxGoodsVO.getName() != null) {
            criteria.andNameLike("%" + xcxGoodsVO.getName() + "%");;
        }
        //上下架状态
        if (xcxGoodsVO.getStatus() != null) {
            criteria.andStatusEqualTo(xcxGoodsVO.getStatus());
        }
        //商品分类
        if (xcxGoodsVO.getCateId() != null) {
            criteria.andCateIdEqualTo(xcxGoodsVO.getCateId());
        }
        //店铺ID
        if (xcxGoodsVO.getShopId() != null) {
            criteria.andShopIdEqualTo(xcxGoodsVO.getShopId());
        }
        criteria.andDelStatusEqualTo(CommonConsts.NO);

        page.setTotalCount(xcxGoodsMapper.countByExample(example));
        example.setLimitStart(page.limitStart());
        example.setLimitEnd(page.limitEnd());
        example.setOrderByClause("update_time desc");
        return xcxGoodsMapper.selectByExample(example);
    }

    public int batchUpdateGoodsStatus(String xcxGoodsId, Integer status) {
        XcxGoodsExample example = new XcxGoodsExample();
        List<Integer> goodsIdList = new ArrayList<>();

        if (status == null && status != 1 && status != 2) {
            throw new ServiceException("上下架状态有误");
        }
        String[] array = xcxGoodsId.split(",");
        for (int i = 0; i < array.length; i++) {
            goodsIdList.add(Integer.parseInt(array[i]));
        }
        XcxGoods xcxGoods = new XcxGoods();
        xcxGoods.setStatus(status);
        xcxGoods.setUpdateTime(new Date());
        example.createCriteria().andGoodsIdIn(goodsIdList);
        return updateByExampleSelective(xcxGoods, example);
    }

    public int batchUpdateGoodsDelStatus(String xcxGoodsId, Integer delStatus) {
        XcxGoodsExample example = new XcxGoodsExample();
        List<Integer> goodsIdList = new ArrayList<>();
        String[] array = xcxGoodsId.split(",");
        for (int i = 0; i < array.length; i++) {
            goodsIdList.add(Integer.parseInt(array[i]));
        }
        XcxGoods xcxGoods = new XcxGoods();
        xcxGoods.setDelStatus(delStatus);
        xcxGoods.setUpdateTime(new Date());
        example.createCriteria().andGoodsIdIn(goodsIdList);
        return updateByExampleSelective(xcxGoods, example);
    }

    public int updateGoodsInfoById(XcxGoodsVO xcxGoodsVO) {
        XcxGoodsExample example = new XcxGoodsExample();
        example.createCriteria().andGoodsIdEqualTo(xcxGoodsVO.getGoodsId());
        XcxGoods xcxGoods = new XcxGoods();
        xcxGoods.setName(xcxGoodsVO.getName());
        xcxGoods.setParam(xcxGoodsVO.getParam());
        xcxGoods.setDetail(xcxGoodsVO.getDetail());
        xcxGoods.setWeight(xcxGoodsVO.getWeight());
        xcxGoods.setSpec(xcxGoodsVO.getSpec());
        xcxGoods.setTag(xcxGoodsVO.getTag());
        xcxGoods.setStatus(xcxGoodsVO.getStatus());
        xcxGoods.setCateId(xcxGoodsVO.getCateId());
        xcxGoods.setImage(xcxGoodsVO.getImage());
        xcxGoods.setThumImage(xcxGoodsVO.getThumImage());
        xcxGoods.setStockNum(xcxGoodsVO.getStockNum());
        xcxGoods.setSaleNum(xcxGoodsVO.getSaleNum());
        return updateByExampleSelective(xcxGoods,example);
    }

    public List<XcxGoods> listByIds(List<Integer> goodsIds) {
        XcxGoodsExample example = new XcxGoodsExample();
        example.createCriteria().andGoodsIdIn(goodsIds);
        return selectByExample(example);
    }
    public List<XcxGoods> listByIds(List<Integer> goodsIds,Integer shopId) {
        XcxGoodsExample example = new XcxGoodsExample();
        example.createCriteria().andGoodsIdIn(goodsIds);
        return selectByExample(example);
    }
}
